//
//  BSKTypeSelecter.m
//  MicroMemory
//
//  Created by rimi on 16/7/19.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "BSKTypeSelecter.h"
#import "BSKconsts.h"
#import "BSKNoteType.h"
#import "UIView+SDAutoLayout.h"
#import "BSKTheme.h"

@interface BSKTypeSelecter ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)NSArray * typlist;
@property(nonatomic,copy)void(^selectedCallBack)(NSString * typeID);
@property(nonatomic,strong) UITableView * table;
@property(nonatomic,strong) UIView * ContentView;
@end

@implementation BSKTypeSelecter

- (instancetype)initWithTyps:(NSArray *)array selectedCallBack:(void(^)(NSString * typeID))callback
{
	self = [super init];
	if (self) {
		_typlist = array;
		_selectedCallBack = callback;
		[self loadUI];
	}
	return self;
}
-(void)loadUI{
	self.frame = [UIScreen mainScreen].bounds;
	UIView * bgView = [[UIView alloc]initWithFrame:self.bounds];
	self.backgroundColor = [UIColor clearColor];
	bgView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
	[self addSubview:bgView];
	
	_ContentView = [[UIView alloc]init];
	_ContentView.center = CGPointMake(ScreenWidth/2, ScreenHeight/2);
	_ContentView.backgroundColor = [UIColor whiteColor];
	
	CGFloat ContentHeight = 40*_typlist.count+50;
	if (ContentHeight > ScreenHeight * 0.7) {
		ContentHeight = ScreenHeight*0.7;
	}
	_ContentView.bounds = CGRectMake(0, 0, ScreenWidth*0.7,  ContentHeight);
	_ContentView.layer.masksToBounds = YES;
	_ContentView.layer.cornerRadius = 20;
	[self addSubview:_ContentView];
	_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _ContentView.bounds.size.width, 50)];
	_titleLabel.textAlignment = NSTextAlignmentCenter;
	_titleLabel.textColor = [UIColor whiteColor];
	_titleLabel.backgroundColor = [BSKTheme curentTheme].noteBackgroundColor;
	[_ContentView addSubview:_titleLabel];

	self.alpha = 0;
	_table = [[UITableView alloc]init];
	_table.frame = CGRectMake(0, 50, _ContentView.bounds.size.width, _ContentView.bounds.size.height-50);
	_table.dataSource = self;
	_table.delegate = self;
	_table.rowHeight = 40;
	UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
	[bgView addGestureRecognizer:tapGesture];
	[self.ContentView addSubview:_table];
}

-(void)show{
	self.transform = CGAffineTransformMakeScale(1.5, 1.5);
	[UIView animateWithDuration:0.25 animations:^{
		self.transform = CGAffineTransformMakeScale(1, 1);
		self.alpha = 1;
	} completion:^(BOOL finished) {
		
	}];
}

-(void)hide{
	self.transform = CGAffineTransformMakeScale(1, 1);
	[UIView animateWithDuration:0.25 animations:^{
		self.transform = CGAffineTransformMakeScale(1.5, 1.5);
		self.alpha = 0;
	} completion:^(BOOL finished) {
		[self removeFromSuperview];
	}];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return _typlist.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"typecell"];
	if (!cell) {
		cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"typecell"];
	}
	cell.textLabel.sd_layout.leftSpaceToView(cell.textLabel.superview,ScreenWidth*0.15);
	BSKNoteType * type = _typlist[indexPath.row];
	cell.textLabel.text = type.typeName;
	if (type.secretMode) {
		cell.imageView.image =[self imageResize:[UIImage imageNamed:@"key"] ToSize:CGSizeMake(30, 30)] ;
		cell.imageView.backgroundColor = [BSKTheme curentTheme].noteBackgroundColor;
		cell.imageView.layer.cornerRadius = 10;
		cell.separatorInset = UIEdgeInsetsMake(5, 5, 5, 5);
		cell.imageView.layer.masksToBounds = YES;
	}
	return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	NSLog(@"选择了分类");
	if (_selectedCallBack != nil) {
		BSKNoteType * type = _typlist[indexPath.row];
		_selectedCallBack(type.typeID);
	}
	[self hide];
}

- (UIImage*)imageResize:(UIImage*)image ToSize:(CGSize)size
{
	UIGraphicsBeginImageContext(size);
	[image drawInRect:CGRectMake(0,0,size.width,size.height)];
	UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return newImage;
}

@end
